﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using UPMS.Core.Dao;
using UPMS.Core.Domain;

namespace UPMS.Web.Controllers
{
    public class HostController : BaseController
    {
        // GET: /Host/
        public ActionResult Index()
        {
            return View(hostDao.Select());
        }

        // GET: /Host/Details/5
        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            Host host = hostDao.Select(id.Value);

            if (host == null)
            {
                return HttpNotFound();
            }
            return View(host);
        }

        // GET: /Host/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Host/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,IP,Description,Created,CreatedBy,Modified,ModifiedBy")] Host host)
        {
            if (ModelState.IsValid)
            {
                host.Id = Guid.NewGuid();
                hostDao.Insert(host);
                return RedirectToAction("Index");
            }

            return View(host);
        }

        // GET: /Host/Edit/5
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            Host host = hostDao.Select(id.Value);

            if (host == null)
            {
                return HttpNotFound();
            }
            return View(host);
        }

        // POST: /Host/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Name,IP,Description,Created,CreatedBy,Modified,ModifiedBy")] Host host)
        {
            if (ModelState.IsValid)
            {
                hostDao.Update(host);
                return RedirectToAction("Index");
            }

            return View(host);
        }

        // GET: /Host/Delete/5
        public ActionResult Delete(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            Host host = hostDao.Select(id.Value);

            if (host == null)
            {
                return HttpNotFound();
            }
            return View(host);
        }

        // POST: /Host/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(Guid id)
        {
            hostDao.Delete(id);

            return RedirectToAction("Index");
        }

    }
}
